package com.linsen.core.mybatisplus.config;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.linsen.core.common.annotation.AutoConfiguration;
import com.linsen.core.common.factory.YamlPropertySourceFactory;
import com.linsen.core.mybatisplus.handler.LinsenMetaObjectHandler;
import com.linsen.core.mybatisplus.handler.LinsenTenantLineHandler;
import com.linsen.core.mybatisplus.props.LinsenMyBatisPlusProperties;
import com.linsen.core.redis.util.RedisSecurityUtil;
import com.linsen.uaa.provider.feign.IAuthFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

/**
 * @author zhaoyong
 * @date 2023/3/11
 */
@AutoConfiguration
@RequiredArgsConstructor
@EnableConfigurationProperties({LinsenMyBatisPlusProperties.class})
@PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:linsen-mybatisplus.yml")
public class LinsenMyBatisPlusAutoConfiguration {
    private final LinsenMyBatisPlusProperties myBatisPlusProperties;

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(LinsenTenantLineHandler tenantLineHandler) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        // 溢出总页数后是否进行处理
        paginationInnerInterceptor.setOverflow(Boolean.FALSE);
        // 单页分页条数限制(默认无限制)
        paginationInnerInterceptor.setMaxLimit(500L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        // 乐观锁插件
        // interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 多租户插件
        if (this.myBatisPlusProperties.getTenantLineHandler()) {
            interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantLineHandler));
        }
        // 防全表更新与删除插件
        // interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }


    @Bean
    public LinsenMetaObjectHandler metaObjectHandler(IAuthFeignClient authFeignClient, RedisSecurityUtil redisSecurityUtil, LinsenMyBatisPlusProperties myBatisPlusProperties) {
        return new LinsenMetaObjectHandler(authFeignClient, redisSecurityUtil, myBatisPlusProperties);
    }

    @Bean
    public LinsenTenantLineHandler tenantLineHandler(LinsenMyBatisPlusProperties myBatisPlusProperties) {
        return new LinsenTenantLineHandler(myBatisPlusProperties);
    }
}
